//
//  ShoperCarConfirmOrderTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/8/4.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "CarConfirmOrderController.h"
#import "ConfirmOrderController.h"
#import "ConfirmView.h"
#import "ShopSecondCell.h"
#import "ConfirmOrderCell.h"

#import "AddressManagerController.h"
#import "OrderPaymentController.h"
#import "ProductInfo.h"
#import "AddressModel.h"
#import "AddressTableViewCell.h"

@interface CarConfirmOrderController ()<UITableViewDelegate,UITableViewDataSource,ConfirmViewOrderDelagate,ThreeViewControllerDelegate>

{
    NSInteger index;
    AddressModel *address;
    BOOL  judgeAddress;
    NSMutableArray *allProductArray;
}

@property(nonatomic,strong)UITableView *confirmOrderView;



@end

@implementation CarConfirmOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.productArray)
    
    self.title = @"确认订单";
    index = self.productArray.count;
    [self addUI];
    NSLog(@"%@",self.allPrice);
    judgeAddress = NO;
}

- (void)addUI
{

    UITableView *confirmOrderView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT * 0.8) style:UITableViewStyleGrouped];
    self.confirmOrderView = confirmOrderView;
    [self.view addSubview:confirmOrderView];
    confirmOrderView.delegate = self;
    confirmOrderView.dataSource = self;
    
<<<<<<< HEAD
    ConfirmView *lastView = [[ConfirmView alloc] initWithFrame:CGRectMake(0,HEIGHT/9*8-64,WIDTH,HEIGHT/9)];
=======
    ConfirmView *lastView = [[ConfirmView alloc] initWithFrame:CGRectMake(0,HEIGHT * 0.8,WIDTH,HEIGHT/9)];
>>>>>>> d3592b511e827f55e5327fb3b6299cf02ab340d2
    lastView.delegate = self;
    lastView.totalPrice.text = [NSString stringWithFormat:@"总价:%@",self.allPrice];
    [self.view addSubview:lastView];
}

//订单支付
- (void)confirmViewOrderDidClick:(UIButton *)button
{
    OrderPaymentController *OrderPaymentVC = [[OrderPaymentController alloc] init];
    [self.navigationController pushForm:self.navigationController pushToAndHidden:OrderPaymentVC];
}

#pragma mark>>>>>>>>DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else{
        return self.productArray.count + 4;
//        NSLog(@"%ld",self.productArray.count);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",self.productArray.count);
    
    if (indexPath.section == 0) {
        
        if (address) {
            AddressTableViewCell *addressCell = [tableView dequeueReusableCellWithIdentifier:@"address"];
            
            if (addressCell == nil) {
                addressCell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"address"];
                addressCell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            addressCell.imageView.image = [UIImage imageNamed:@"confirmOrder"];
            addressCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            addressCell.nameLabel.text = [NSString stringWithFormat:@"收货人: %@",address.name];
            addressCell.phoneNumberLabel.text = address.phone;
            
            addressCell.addressLabel.text = [NSString stringWithFormat:@"详细地址: %@%@%@%@",address.province,address.city,address.county,address.address];
            
            return addressCell;
            
        } else {
            
            UITableViewCell *firstCell=[tableView dequeueReusableCellWithIdentifier:@"first"];
            
            if (firstCell==nil) {
                firstCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"first"];
                firstCell.selectionStyle = UITableViewCellSelectionStyleNone;
                firstCell.textLabel.font = FONT(15);
                firstCell.detailTextLabel.font = FONT(15);
            }

            
            firstCell.textLabel.text = @"请填写收货地址";
            firstCell.imageView.image = [UIImage imageNamed:@"confirmOrder"];
            firstCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return firstCell;
        }
        
    } else {
        if (indexPath.row < self.productArray.count) {
            ConfirmOrderCell *confirmOrderCell = [tableView dequeueReusableCellWithIdentifier:@"ConfirmOrder"];
            if (confirmOrderCell == nil) {
                confirmOrderCell = [[ConfirmOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ConfirmOrder"];
            }
            
            /*
             *orderImg;
             *orderName;
             *orderColorSize;
             *orderNumber;
             *orderPrice;
             */
            
            ProductInfo *model = self.productArray[indexPath.row];
            NSString *string = [NSString stringWithFormat:@"%@%@",model.picprex[@"s"],model.picurl];
            [confirmOrderCell.orderImg sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
            
            confirmOrderCell.orderName.text = model.title;
            
            NSString *standard;
            
            if (model.yanse.length == 0 && model.chima.length == 0) {
                standard = @"";
            } else {
                standard = [NSString stringWithFormat:@"颜色:%@ 尺码:%@",model.yanse,model.chima];
            }
            
            confirmOrderCell.orderColorSize.text = standard;
            
            confirmOrderCell.orderNumber.text = [NSString stringWithFormat:@"数量:%ld",model.nums];
            
            confirmOrderCell.orderPrice.text = [NSString stringWithFormat:@"¥:%@",model.jiage];
            
            return confirmOrderCell;
            
        } else {
            static NSString *identifier= @"ConfirmOrderCell";
            
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (cell==nil) {
                cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.textLabel.font = FONT(15);
                cell.detailTextLabel.font = FONT(15);
            }
            
            
            //            NSLog(@"%ld",indexPath.row - index);
            //            NSLog(@"%ld",indexPath.row);
            NSArray *arr = @[@"配送方式 : ",@"快递运费 : ",@"费率 : ",@"价格合计 : "];
            
            cell.textLabel.text = arr[indexPath.row - index];
            
            cell.detailTextLabel.text = @"快递";
            cell.textLabel.textColor = [UIColor grayColor];
            
            return cell;
            
        }

    }
    
}



#pragma mark>>>>>>>>Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
    NSLog(@"%@",self.productArray);
    if (indexPath.section == 0) {
        AddressManagerController *AddressManagerVC = [[AddressManagerController alloc] init];
        AddressManagerVC.delegate = self;
        AddressManagerVC.productArray = self.productArray;
        [self.navigationController pushForm:self.navigationController pushToAndHidden:AddressManagerVC];
    }
}

- (void)addViewController:(AddressManagerController *)addViewContrioller addUser:(AddressModel *)userDict andProductArray:(NSMutableArray *)productArray {
    address = userDict;
    NSLog(@"%@",userDict);
//    NSLog(@"%@",productArray);
//    NSLog(@"%@",self.productArray);
    [self.confirmOrderView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        if (address) {
            return HEIGHT/9 + 10;
        } else {
            return HEIGHT/10;
        }
    }else if (indexPath.section == 1 && indexPath.row < self.productArray.count){
        
#warning 为iPhone 7 留后路
        if (iPhone5) {
            return HEIGHT/6;
        }else if (iPhone6){
            return HEIGHT/6.5;
        }else{
            return HEIGHT/7;
        }
        
    }else{
        return HEIGHT/13;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSMutableArray *)productArray {
    if (!_productArray) {
        _productArray = [NSMutableArray array];
    }
    return _productArray;
}

@end
